const TransactionService = require("../services/transactionService");

class TransactionController {
  // 创建交易
  async createTransaction(ctx, next) {
    try {
      const { amount, description } = ctx.request.body;
      const transaction = await TransactionService.createTransaction({
        amount,
        description,
        userId: ctx.state.user.id,
      });
      ctx.status = 201;
      ctx.body = { success: true, data: transaction };
    } catch (error) {
      console.error("创建交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "创建交易时出错" };
    }
  }
  
  // 获取所有交易
  async getAllTransactions(ctx, next) {
    try {
      const transactions = await TransactionService.getAllTransactions();
      ctx.status = 200;
      ctx.body = { success: true, data: transactions };
    } catch (error) {
      console.error("获取所有交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "获取所有交易时出错" };
    }
  }

  // 获取特定交易
  async getTransactionById(ctx, next) {
    try {
      const { id } = ctx.params;
      const transaction = await TransactionService.getTransactionById(id);
      if (!transaction) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该交易" };
        return;
      }
      ctx.status = 200;
      ctx.body = { success: true, data: transaction };
    } catch (error) {
      console.error("获取特定交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "获取特定交易时出错" };
    }
  }

  // 更新交易
  async updateTransaction(ctx, next) {
    try {
      const { id } = ctx.params;
      const { amount, description } = ctx.request.body;
      const result = await TransactionService.updateTransaction({
        id,
        amount,
        description,
      });
      if (!result) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该交易" };
        return;
      }
      ctx.status = 200;
      ctx.body = { success: true };
    } catch (error) {
      console.error("更新交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "更新交易时出错" };
    }
  }

  // 删除交易
  async deleteTransaction(ctx, next) {
    try {
      const { id } = ctx.params;
      const result = await TransactionService.deleteTransaction(id);
      if (!result) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该交易" };
        return;
      }
      ctx.status = 200;
      ctx.body = { success: true };
    } catch (error) {
      console.error("删除交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "删除交易时出错" };
    }
  }

  // 获取用户自己的交易
  async getUserTransactions(ctx, next) {
    try {
      const transactions = await TransactionService.getUserTransactions(
        ctx.state.user.id
      );
      ctx.status = 200;
      ctx.body = { success: true, data: transactions };
    } catch (error) {
      console.error("获取用户自己的交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "获取用户自己的交易时出错" };
    }
  }

  // 用户删除自己的交易
  async deleteUserTransaction(ctx, next) {
    try {
      const { id } = ctx.params;
      const result = await TransactionService.deleteUserTransaction(
        id,
        ctx.state.user.id
      );
      if (!result) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该交易" };
        return;
      }
      ctx.status = 200;
      ctx.body = { success: true };
    } catch (error) {
      console.error("用户删除自己的交易时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "用户删除自己的交易时出错" };
    }
  }
}

module.exports = new TransactionController();
